package site.nullable.recorder.service

import android.media.MediaPlayer
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import java.io.IOException

class AudioPlayerService {
    private var mediaPlayer: MediaPlayer? = null
    private var currentPlayingPath: String? = null

    // 添加播放状态的可观察流
    private val _playbackState = MutableStateFlow<PlaybackState>(PlaybackState.Stopped)
    val playbackState: StateFlow<PlaybackState> = _playbackState.asStateFlow()

    // 添加当前播放路径的可观察流
    private val _currentPlayingPathFlow = MutableStateFlow<String?>(null)
    val currentPlayingPathFlow: StateFlow<String?> = _currentPlayingPathFlow.asStateFlow()

    sealed class PlaybackState {
        object Stopped : PlaybackState()
        object Playing : PlaybackState()
        object Paused : PlaybackState()
    }

    fun playRecording(filePath: String, onCompletion: (() -> Unit)? = null): Boolean {
        return try {
            stopPlaying()

            mediaPlayer = MediaPlayer().apply {
                setDataSource(filePath)
                prepare()
                start()

                setOnCompletionListener {
                    _playbackState.value = PlaybackState.Stopped
                    _currentPlayingPathFlow.value = null
                    onCompletion?.invoke()
                }
            }

            currentPlayingPath = filePath
            _currentPlayingPathFlow.value = filePath
            _playbackState.value = PlaybackState.Playing
            true
        } catch (e: IOException) {
            e.printStackTrace()
            _playbackState.value = PlaybackState.Stopped
            _currentPlayingPathFlow.value = null
            false
        } catch (e: IllegalStateException) {
            e.printStackTrace()
            _playbackState.value = PlaybackState.Stopped
            _currentPlayingPathFlow.value = null
            false
        }
    }

    fun stopPlaying() {
        mediaPlayer?.apply {
            if (isPlaying) {
                stop()
            }
            release()
        }
        mediaPlayer = null
        currentPlayingPath = null
        _currentPlayingPathFlow.value = null
        _playbackState.value = PlaybackState.Stopped
    }

    fun pausePlaying() {
        mediaPlayer?.apply {
            if (isPlaying) {
                pause()
                _playbackState.value = PlaybackState.Paused
            }
        }
    }

    fun resumePlaying(): Boolean {
        return try {
            mediaPlayer?.apply {
                if (!isPlaying) {
                    start()
                    _playbackState.value = PlaybackState.Playing
                    return true
                }
            }
            false
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }

    fun isPlaying(): Boolean = mediaPlayer?.isPlaying ?: false

    fun getCurrentPlayingPath(): String? = currentPlayingPath

    fun getCurrentPosition(): Int = mediaPlayer?.currentPosition ?: 0

    fun getDuration(): Int = mediaPlayer?.duration ?: 0

    fun isPlaying(filePath: String): Boolean {
        return isPlaying() && currentPlayingPath == filePath
    }
}